#include "domaindetailinfodlg.h"
#include "ui_domaindetailinfodlg.h"


//#include <QApplication>
//#include <QDialog>
//#include <QVBoxLayout>
//#include <QGroupBox>
//#include <QLabel>
//#include <QPushButton>
//#include <QHBoxLayout>
//#include <QScrollArea>
//#include <QStyle>
//#include "tdomain.h"

class GroupBoxWidget : public QGroupBox {
    //   Q_OBJECT
public:
    GroupBoxWidget(const QMap<QString, QString> &data,const QString & title, QWidget *parent = nullptr) :
        QGroupBox(parent)
    {
        this->setTitle(title);

        int row = 0;
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        //        mainLayout->setContentsMargins(10, 10, 10, 10);

//        QPushButton *toggleButton = new QPushButton(this);
//        toggleButton->setCheckable(true);
//        toggleButton->setFixedSize(20, 20);
//        toggleButton->move(width() - toggleButton->width() - 2, 2);
//        toggleButton->setIcon(style()->standardIcon(QStyle::SP_DesktopIcon));
//        connect(toggleButton, &QPushButton::toggled, this, &GroupBoxWidget::toggleContent);

        // 创建一个新的容器 widget 来承载内容
        _contentWidget = new QWidget(this);
        contentLayout = new QVBoxLayout(_contentWidget);
        QMap<QString, QString> pdata = data;

        if(pdata.size()%2!=0)
        {
            pdata["-"]="-";
        }

        QHBoxLayout *hLayout = nullptr; // 在这里声明
        for (auto it = pdata.begin(); it != pdata.end(); ++it) {
            if (row % 2 == 0) { // 新的一行
                if (hLayout != nullptr) { // 如果这不是第一行，先添加上一行的布局
                    contentLayout->addLayout(hLayout);
                }
                hLayout = new QHBoxLayout();
                hLayout->setSpacing(10);
            }
            QLabel *keyLabel = new QLabel(it.key(), this);
            keyLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
            //keyLabel->setAlignment(Qt::AlignRight);
            hLayout->addWidget(keyLabel,1);

            QLabel *valueLabel = new QLabel(it.value(), this);
            valueLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
            //valueLabel->setAlignment(Qt::AlignRight);
            hLayout->addWidget(valueLabel,2);
            row++;
        }

        // 添加最后一行布局
        if (hLayout != nullptr) {
            contentLayout->addLayout(hLayout);
        }
        mainLayout->addWidget(_contentWidget);
    }

private slots:
    void toggleContent(bool checked)
    {
        if (checked) {
            _contentWidget->show();
            static_cast<QPushButton*>(sender())->setIcon(style()->standardIcon(QStyle::SP_DesktopIcon));

        } else {
            _contentWidget->hide();
            static_cast<QPushButton*>(sender())->setIcon(style()->standardIcon(QStyle::SP_TitleBarNormalButton));

        }
    }

private:
    QLabel *m_groupContentLabel;
    QWidget *_contentWidget; // 新增的内容容器 widget
    QVBoxLayout *contentLayout ;
};

class NGroupBoxWidget : public QGroupBox {
    //   Q_OBJECT
public:
    NGroupBoxWidget(const QList<KeyValue> &data,const QString & title, QWidget *parent = nullptr) :
        QGroupBox(parent)
    {
        this->setTitle(title);

        int row = 0;
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        //        mainLayout->setContentsMargins(10, 10, 10, 10);

        //        QPushButton *toggleButton = new QPushButton(this);
        //        toggleButton->setCheckable(true);
        //        toggleButton->setFixedSize(20, 20);
        //        toggleButton->move(width() - toggleButton->width() - 2, 2);
        //        toggleButton->setIcon(style()->standardIcon(QStyle::SP_DesktopIcon));
        //        connect(toggleButton, &QPushButton::toggled, this, &GroupBoxWidget::toggleContent);

        // 创建一个新的容器 widget 来承载内容
        _contentWidget = new QWidget(this);
        contentLayout = new QVBoxLayout(_contentWidget);
        QList<KeyValue> pdata = data;

        if(pdata.size()%2!=0)
        {
            pdata.append(KeyValue{"-","-"});
        }

        QHBoxLayout *hLayout = nullptr; // 在这里声明
        for (int i=0;i<pdata.size();i++) {
            if (row % 2 == 0) { // 新的一行
                if (hLayout != nullptr) { // 如果这不是第一行，先添加上一行的布局
                    contentLayout->addLayout(hLayout);
                }
                hLayout = new QHBoxLayout();
                hLayout->setSpacing(10);
            }
            QLabel *keyLabel = new QLabel(pdata.at(i).key, this);
            keyLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
            //keyLabel->setAlignment(Qt::AlignRight);
            hLayout->addWidget(keyLabel,1);

            QLabel *valueLabel = new QLabel(pdata.at(i).value, this);
            valueLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
            //valueLabel->setAlignment(Qt::AlignRight);
            hLayout->addWidget(valueLabel,2);
            row++;
        }

        // 添加最后一行布局
        if (hLayout != nullptr) {
            contentLayout->addLayout(hLayout);
        }
        mainLayout->addWidget(_contentWidget);
    }

private slots:
    void toggleContent(bool checked)
    {
        if (checked) {
            _contentWidget->show();
            static_cast<QPushButton*>(sender())->setIcon(style()->standardIcon(QStyle::SP_DesktopIcon));

        } else {
            _contentWidget->hide();
            static_cast<QPushButton*>(sender())->setIcon(style()->standardIcon(QStyle::SP_TitleBarNormalButton));

        }
    }

private:
    QLabel *m_groupContentLabel;
    QWidget *_contentWidget; // 新增的内容容器 widget
    QVBoxLayout *contentLayout ;
};

DomainDetailInfoDlg::DomainDetailInfoDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DomainDetailInfoDlg)
{
    ui->setupUi(this);

    setWindowTitle("域名详细信息");
//    setMinimumSize(1200, 800);
//    setMaximumSize(1200, 800);




}

DomainDetailInfoDlg::~DomainDetailInfoDlg()
{
    delete ui;
}

void DomainDetailInfoDlg::initDlgData(DomainDetailInfo ddi)
{
//    qDebug()<<"initDlgData,domainId"<<ddi.domainId;
//    qDebug()<<"initDlgData,domainId"<<ddi.domainAdmin["Registry Admin ID"];
//    qDebug()<<"initDlgData,domainId"<<ddi.domainBill["Registry Billing ID"];
//    qDebug()<<"initDlgData,domainId"<<ddi.domainRegistry["Registry Domain ID"];
//    qDebug()<<"initDlgData,domainId"<<ddi.domainTech["Registry Tech ID"];
//    qDebug()<<"initDlgData,domainId"<<ddi.domainNameServer.at(0);
//    qDebug()<<"initDlgData,domainId"<<ddi.domainStatus.at(0);

    QWidget * pSubWidget = new QWidget();

    QScrollArea *pSa = new QScrollArea(this);
    QRect parentGeometry = this->geometry();
    QSize dialogSize = size();
   // qDebug()<<dialogSize;
//    parentGeometry.setTop(0);
//    parentGeometry.setLeft(0);
//    pSa->setGeometry(parentGeometry);
    pSa->setFixedSize(dialogSize.width(), dialogSize.height());
    pSa->setWidgetResizable(true);
    pSa->setWidget(pSubWidget);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 10);

    NGroupBoxWidget *group1 = new NGroupBoxWidget(ddi.domainRegistry, "域名注册");
    mainLayout->addWidget(group1,3);

    NGroupBoxWidget *group2 = new NGroupBoxWidget(ddi.domainAdmin, "域名ADMIN");
    mainLayout->addWidget(group2,2);

    NGroupBoxWidget *group3 = new NGroupBoxWidget(ddi.domainTech, "域名TECH");
    mainLayout->addWidget(group3,2);

    NGroupBoxWidget *group4 = new NGroupBoxWidget(ddi.domainBill, "域名BILL");
    mainLayout->addWidget(group4,2);

    //        GroupBoxWidget *group5 = new GroupBoxWidget(data2, "这里是分组4的标题");
    //        mainLayout->addWidget(group5);

    pSubWidget->setLayout(mainLayout);

}
